home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / file / logiso.000 / logiso / Utils / logiso_get.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-24  |  3.9 KB  |  145 lines

  1.  
  2. /* logiso_get.c  
  3.    Usage:    logiso_get  [ -v ] cd_mount_point [ log_file ]
  4.       Get the isofs usage log and place it in log_file.  The log is written
  5.       to stdout if log_file is not given.
  6.  
  7.       cd_mount_point can be any valid file path at or below the mount
  8.       point of the cd.
  9.  
  10.       If -v is specified, header information is printed, otherwise just
  11.       a list of inode numbers and operation numbers.
  12. */
  13. /* (C) Copyright 1995 by Michael Coulter.  All rights reserved. */
  14.  
  15. #include <fcntl.h>
  16. #include <linux/iso_fs.h>
  17. #include "/usr/include/linux/iso_fs.h"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22.  
  23. #define FALSE        0
  24. #define TRUE        1
  25.  
  26. extern void print_usage();
  27. extern void process_args(int argc, 
  28.              char** argv, 
  29.              FILE** out_fp_p, 
  30.              int* verbose_p,
  31.              char** mount_path_p);
  32.  
  33. main(int argc, char** argv)
  34. {
  35.    struct iso_log_entry*    entry_p;
  36.    struct iso_log_entry*    entry_limit_p;
  37.    int                file_id;
  38.    char*            mount_path;
  39.    FILE*            out_fp;
  40.    struct iso_log_info*        iso_log_info_p;
  41.    int                result;
  42.    int                verbose;
  43.  
  44.    process_args(argc, argv, &out_fp, &verbose, &mount_path);
  45.  
  46.    /* file_id = open("/mnt/system_cd/FileList", O_RDONLY); */
  47.    file_id = open(mount_path, O_RDONLY); 
  48.    if (file_id == -1) {
  49.       fprintf(stderr, "Unable to open file %s.\n", mount_path);
  50.       exit(1);
  51.    }
  52.  
  53.    result = ioctl(file_id, ISO_IOC_GETLOGSIZE);
  54.    if (verbose) {
  55.       fprintf(out_fp, "The size is %d, 0x%x\n", result, result);
  56.    }
  57.    if (result < 0) {
  58.       fprintf(stderr, 
  59.                 "Got size %d, aborting.\n", 
  60.                 result);
  61.       exit(1);
  62.    }
  63.    iso_log_info_p = (struct iso_log_info*) malloc(result);
  64.  
  65.    result = ioctl(file_id, ISO_IOC_READLOG, iso_log_info_p);
  66.    if (result != 0) {
  67.       fprintf(stderr, "Failure to read log, status %d\n", result);
  68.       exit(1);
  69.    }
  70.    if (iso_log_info_p->version != ISO_LOG_VERSION) {
  71.       fprintf(stderr, "Expecting version %d, got %d\n", 
  72.               ISO_LOG_VERSION, 
  73.               iso_log_info_p->version);
  74.       /* exit(2); */
  75.    }
  76.    if (verbose) {
  77.       fprintf(out_fp, "version %d\nbuf_format %d\nnsize %d\ndevice %d\n"
  78.                        "overflow_count %d\nnbr_entries %d\n",
  79.           iso_log_info_p->version,
  80.           iso_log_info_p->buf_format,
  81.           iso_log_info_p->size,
  82.           iso_log_info_p->device,
  83.           iso_log_info_p->overflow_count,
  84.           iso_log_info_p->nbr_entries);
  85.    }
  86.  
  87.    entry_p = &iso_log_info_p->log_entry[0];
  88.    entry_limit_p = entry_p + iso_log_info_p->nbr_entries;
  89.    while (entry_p < entry_limit_p) {
  90.       if (entry_p->device == iso_log_info_p->device) {
  91.      fprintf(out_fp, "%ld        %d    %d\n", 
  92.          entry_p->inode, 
  93.          entry_p->operation,
  94.          entry_p->device);
  95.       }
  96.       entry_p++;
  97.    }
  98.    fclose(out_fp);
  99.    return 0;
  100. }
  101.  
  102. void print_usage() 
  103. {
  104.  
  105. char usage_str[] =
  106. "   Usage:    logiso_get  [ -v ] cd_mount_point [ log_file ]\
  107.       Get the isofs usage log and place it in log_file.  The log is written\
  108.       to stdout if log_file is not given.\
  109. \
  110.       cd_mount_point can be any valid file path at or below the mount\
  111.       point of the cd.\
  112. \
  113.       If -v is specified, header information is printed, otherwise just\
  114.       a list of inode numbers and operation numbers.\
  115. ";
  116.    fprintf(stderr, "%s\n", usage_str);
  117. } /* end print_usage */
  118.  
  119. void process_args(int argc, 
  120.           char** argv, 
  121.           FILE** out_fp_p, 
  122.           int* verbose_p, 
  123.           char** mount_path_p)
  124. {
  125.    argc--; argv++;  /* skip command name */
  126.    *verbose_p = FALSE;
  127.    if (argc >= 1 && strcmp(*argv, "-v") == 0) {
  128.       argc--; argv++;  /* done with -v */
  129.       *verbose_p = TRUE;
  130.    }
  131.    if (argc < 1 ) {
  132.       print_usage();
  133.       fprintf(stderr, "No cd_mount_point was provided.\n");
  134.       exit(1);
  135.    }
  136.    *mount_path_p = *argv;  argc--;
  137.    *out_fp_p = stdout;
  138.    if (argc >= 1) {
  139.       if ((*out_fp_p = fopen(*argv, "w") ) == NULL) {
  140.          fprintf(stderr, "Unable to open %s\n", *argv);
  141.      argc--; argv++;  /* done with -v */
  142.       }
  143.    }
  144. } /* end process_args */
  145.